# gui/styles/style_manager.py
"""
样式管理器 - 统一管理QSS样式文件
"""

import os
from PyQt5.QtCore import QDir

class StyleManager:
    """样式管理器"""

    def __init__(self):
        self.styles_dir = os.path.join(os.path.dirname(__file__))
        self._cached_styles = {}

    def load_style(self, style_name):
        """加载指定的样式文件"""
        if style_name in self._cached_styles:
            return self._cached_styles[style_name]

        style_file = os.path.join(self.styles_dir, f"{style_name}.qss")

        if not os.path.exists(style_file):
            print(f"警告: 样式文件 {style_file} 不存在")
            return ""

        try:
            with open(style_file, 'r', encoding='utf-8') as f:
                style_content = f.read()
                self._cached_styles[style_name] = style_content
                return style_content
        except Exception as e:
            print(f"错误: 无法加载样式文件 {style_file}: {e}")
            return ""

    def get_style(self, style_name):
        """获取样式内容"""
        return self.load_style(style_name)

    def reload_style(self, style_name):
        """重新加载指定样式"""
        if style_name in self._cached_styles:
            del self._cached_styles[style_name]
        return self.load_style(style_name)

    def clear_cache(self):
        """清空样式缓存"""
        self._cached_styles.clear()

# 全局样式管理器实例
style_manager = StyleManager()